package com.cloudpivot.receivales.contract.service.impl;

import com.cloudpivot.receivales.contract.entity.erpcontract.*;
import com.cloudpivot.receivales.contract.domain.oa.OAContract;
import com.cloudpivot.receivales.contract.domain.oa.OAContractItem;
import com.cloudpivot.receivales.contract.service.OAEntityToU8Entity;
import com.cloudpivot.receivales.contract.util.StrUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import static com.cloudpivot.receivales.contract.util.StrUtil.getDateNow;
import static com.cloudpivot.receivales.contract.util.StrUtil.newGuid;

/**
 * oa对象转换实现类
 */
@Service
public class OAEntityToU8EntityImpl implements OAEntityToU8Entity {
    @Autowired
    private ErpContractServiceImpl erpContractService;

    @Autowired
    private InventoryServiceImpl inventoryService;

    /**
     * oa 合同对象 转 u8合同对象
     *
     * @param inEntity
     * @return
     */
    @Override
    public Map<String, Object> toConvertEntity(OAContract inEntity) {
        Map<String, Object> objectMap = new HashMap<>();
        /**
         * 获取guid+可用合同编号
         */
        String guid = newGuid();
        String strContractID = erpContractService.getLastContractId();
        objectMap.put("strContractID", strContractID);

        /**
         * 转换cmlist
         */
        objectMap.put("cmlist", createCMList(inEntity, guid, strContractID));
        /**
         * 转换cmcontractmain
         */
        objectMap.put("cmcontractmain", createCMContractMain(inEntity, strContractID));
        /**
         * 转换cmcontractB
         */
        objectMap.put("cmcontractb", createCMContractB(inEntity, guid, strContractID));
        /**
         * 转换cmcontractB+ex
         */
        objectMap.put("cmcontractbex", createCMContractBExtradefine(inEntity, guid));
        /**
         * 转换cmcontractItemb+ex
         */
        Map<String, Object> tmpMap = createCMContractItemb(inEntity, guid, strContractID);
        objectMap.put("cmcontractitems", tmpMap.get("CMContractItemB"));
        objectMap.put("cmcontractitemexs", tmpMap.get("CMContractItemBExtradefine"));

        return objectMap;
    }

    /**
     * 创建 CMlist 对象
     * 传入合同id,guid为主
     *
     * @param inEntity
     * @return
     */
    private CMList createCMList(OAContract inEntity, String guid, String strContractID) {
        CMList cmList = new CMList();
        cmList.setGuid(guid);
        cmList.setStrContractID(strContractID);
        cmList.setStrState("1");
        cmList.setDtDate(getDateNow());
        cmList.setStrContractName(inEntity.getHetongmingcheng());
        return cmList;
    }

    /**
     * 创建 CMContractMain 对象
     * 只需要传统一的合同id即可
     *
     * @param inEntity
     * @return
     */
    private CMContractMain createCMContractMain(OAContract inEntity, String strContractID) {
        CMContractMain cmContractMain = new CMContractMain();
        cmContractMain.setStrContractID(strContractID);
        return cmContractMain;
    }


    /**
     * 创建U8合同主表对象
     *
     * @param inEntity
     * @return
     */
    private CMContractB createCMContractB(OAContract inEntity, String guid, String strContractID) {
        CMContractB cmContractB = new CMContractB();
        cmContractB.setGUID(guid);
        cmContractB.setStrContractID(strContractID);
        cmContractB.setStrContractName(inEntity.getHetongmingcheng());
        cmContractB.setStrContractKind("销售类合同");
        cmContractB.setStrContractDesc("流程:"+inEntity.getLiuchengbh()+"-requestid:"+inEntity.getRequestid());
        cmContractB.setCDefine10(inEntity.getZuizhongkh());
        cmContractB.setCDefine11(inEntity.getShenqingrgs());
        //存放位置
        cmContractB.setCDefine13(inEntity.getCunfangwz());
        cmContractB.setStrBisectionUnit(inEntity.getKehumc());
        //转换(合同类别)
        cmContractB.setCDefine12(inEntity.getHetonglb());
        cmContractB.setStrSetupPerson("张梅妮");
        cmContractB.setStrSetupDate(StrUtil.dateTimeToDate(new Date().toString()));
        /**
         * 签订日期
         */
        cmContractB.setStrContractOrderDate(StrUtil.dateTimeToDate(new Date().toString()));
        cmContractB.setDblTotalCurrency(inEntity.getHetongzje());
        cmContractB.setStrContractStartDate(inEntity.getHezuozq());
        cmContractB.setStrContractEndDate(inEntity.getHezuozqjs());
        cmContractB.setStrDeptID(inEntity.getShenqingrbm());
        cmContractB.setStrPersonID(inEntity.getShenqingr());

        return cmContractB;
    }

    /**
     * 创建主表扩展对象
     *
     * @param inEntity
     * @return
     */
    private CMContractBExtradefine createCMContractBExtradefine(OAContract inEntity, String guid) {
        CMContractBExtradefine cmContractBExtradefine = new CMContractBExtradefine();
        cmContractBExtradefine.setGUID(guid);
        cmContractBExtradefine.setChdefine1("否");
        cmContractBExtradefine.setChdefine2("分期收款");
        cmContractBExtradefine.setChdefine22(inEntity.getHetongqdlx());
        return cmContractBExtradefine;
    }

    /**
     * 创建U8合同明细表对象,明细扩展项
     *
     * @param inEntity
     * @return
     */
    private Map<String, Object> createCMContractItemb(OAContract inEntity, String guid, String contractID) {
        List<CMContractItemB> cmContractItemBs = new ArrayList<>();
        List<CMContractItemBExtradefine> cmContractItemBExtradefines = new ArrayList<>();
        //增加 滚动字典
        Map<String, Integer> _idMap = new HashMap<>();
        for (OAContractItem oaContractItem : inEntity.getItems()) {

            String rowGuid = newGuid();
            /**
             明细基础项
             */
            CMContractItemB cmContractItemB = new CMContractItemB();
            cmContractItemB.setGUID(guid);
            cmContractItemB.setRowGUID(rowGuid);
            String invccode=getInvCCode(oaContractItem.getChanpinbm());
            if (StringUtils.isBlank(invccode)) {
                //如果为空 跳过
                continue;
            }else{
                cmContractItemB.setStrCCode(invccode);
            }
            //滚动字典创建
            if(_idMap.containsKey(oaContractItem.getChanpinbm())){
                //已经存在 ++
                String code=oaContractItem.getChanpinbm();
                Integer id=_idMap.get(code);
                id++;
                _idMap.put(code, id);
            }else{
                //不存在
                String code=oaContractItem.getChanpinbm();
                _idMap.put(code, 0);
            }
            //消费序列字典
            String strCode = String.format("%s%s%s", invccode,oaContractItem.getChanpinbm(), _idMap.get(oaContractItem.getChanpinbm())==0?"":_idMap.get(oaContractItem.getChanpinbm()).toString());
            cmContractItemB.setStrCode(strCode);

            cmContractItemB.setStrChief(oaContractItem.getChanpinbm());
            cmContractItemB.setStrName(oaContractItem.getChanpinmc());
            cmContractItemB.setCInvStd(oaContractItem.getGuige());
            cmContractItemB.setDblQuantity(oaContractItem.getShuliang());
            //私有方法 单位取 U8产品关联表单位
            cmContractItemB.setStrMeasureUnit(getComputationUnit(oaContractItem.getChanpinbm()));
            cmContractItemB.setDblPrice(oaContractItem.getBaojiadj());
            cmContractItemB.setStrMemo(oaContractItem.getBeizhu());
            cmContractItemB.setDtStartDate(inEntity.getHezuozq());
            cmContractItemB.setDtEndDate(inEntity.getHezuozqjs());
            cmContractItemB.setStrContractID(contractID);
            cmContractItemBs.add(cmContractItemB);
            /**
             * 明细扩展项
             */
            CMContractItemBExtradefine cmContractItemBExtradefine = new CMContractItemBExtradefine();
            cmContractItemBExtradefine.setRowGUID(rowGuid);
            //V3版本需要增加的字段
            cmContractItemBExtradefine.setCbdefine14(oaContractItem.getGroupId());
            cmContractItemBExtradefine.setCbdefine15(oaContractItem.getGroupName());
            cmContractItemBExtradefine.setCbdefine16(oaContractItem.getMainKeyProductCode());
            cmContractItemBExtradefine.setCbdefine17(oaContractItem.getParentGroupId());
            cmContractItemBExtradefine.setCbdefine18(oaContractItem.getPropor());
            cmContractItemBExtradefines.add(cmContractItemBExtradefine);
        }

        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("CMContractItemB", cmContractItemBs);
        returnMap.put("CMContractItemBExtradefine", cmContractItemBExtradefines);
        return returnMap;
    }

    /**
     * 获取U8产品类别
     * @return
     */
    private String getInvCCode(String strChief){
        if (StringUtils.isBlank(strChief)) {
            return null;
        }
        Inventory inventory =inventoryService.getInventory(strChief);
        if (inventory == null) {
            return null;
        }
        return inventory.getCInvCCode();
    }

    /**
     * 获取U8 产品 档案单位
     * @param cinvcode
     * @return
     */
    private String getComputationUnit(String cinvcode) {
        return inventoryService.getInvUnit(cinvcode);
    }


}
